using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Globalization;
using TVT.Common.Exceptions;
using TVT.Common.Helpers;

namespace TVT.BizLayer.Helpers
{
   
    public class DataTypeParsers
    {
        public static String ToTreeDecimalFormat(Decimal value)
        {
            String result = value.ToString();
            if (result.Contains("."))
                result = result.Substring(0, result.LastIndexOf('.') + 4);
            return result;
        }

        public static int ParseInt(string value)
        {
            if (value == string.Empty)
            {
                return 0;
            }
            else
            {
                return Int32.Parse(value, new CultureInfo(WellKnownKeys.DefaultFilesCulture));
            }
        }

        public static decimal ParseDecimal(string value)
        {
            value = value.Trim();

            if (value == string.Empty)
            {
                return 0;
            }
            else
            {
                return decimal.Parse(value, new CultureInfo(WellKnownKeys.DefaultFilesCulture));
            }
        }

        public static DateTime ParseDateTime(string value)
        {
            try
            {
                if (value == string.Empty)
                {
                    return DateTime.MaxValue;
                }
                else
                {
                    return DateTime.Parse(value, new CultureInfo(WellKnownKeys.DefaultFilesCulture));
                }
            }
            catch (Exception ex)
            {
                return DateTime.MaxValue;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName">YYYYMMDD or 20080829-12:16:27.406</param>
        /// <returns></returns>
        public static DateTime ParseDateTimeFromIce(string date)
        {
            DateTime result;

            int year = ParseInt(date.Substring(0, 4));
            int month = ParseInt(date.Substring(4, 2));
            int day = ParseInt(date.Substring(6, 2)); 

            if (date.Length < 10)
                result = new DateTime( year, month, day);
            else
            {
                String time = date.Substring(date.IndexOf("-") + 1);
                String[] separator = new string[1];
                separator[0] = ":";
                String[] dataArray = time.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                int hour = int.Parse(dataArray[0]);
                int minute = int.Parse(dataArray[1]);
                separator[0] = ".";
                dataArray = dataArray[2].Split(separator, StringSplitOptions.RemoveEmptyEntries);
                int second = int.Parse(dataArray[0]);
                int milisecond = 0;
                try
                {
                    milisecond = int.Parse(dataArray[1]);
                }
                catch (Exception ex)
                { 
                    // No throw need cause the milisend are optional
                }
                result = new DateTime(year, month, day, hour, minute, second, milisecond);
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns>YYYYMMDD</returns>
        public static String ParseDateTimeToYYYYMMDD(DateTime date)
        {
            String year = date.Year.ToString();
            String month = date.Month.ToString();
            String day = date.Day.ToString();

            if(month.Length < 2)
                month = "0" + month;
            if(day.Length < 2)
                day = "0" + day;

            return year + month + day;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns>YYYYMM | YYYYMMDD | YYYYMMwN  w = week </returns>
        public static DateTime ParseMaturity(String date, out bool daily)
        {
            int year;
            int month;
            int day;
            daily = false;

            year = ParseInt(date.Substring(0, 4));
            month = ParseInt(date.Substring(4, 2));

            if ((!date.Contains("w")) && (date.Length > 7))
            {
                day = ParseInt(date.Substring(6, 2)); 
                daily = true;
            }
            else
            {
                day = 1;  
            }
            return new DateTime(year,month,day);
        }

        /// <summary>
        /// Parse a special date format from the Nymex Trades exel file
        /// </summary>
        /// <param name="day">12/31/08</param>
        /// <param name="time">12:00:00</param>
        /// <returns></returns>
        public static DateTime ParseDateTimeFromTradeNymex(String day, String time)
        {
            DateTime result;
            DateTime date = DateTime.Parse(day);
            String[] separator = new string[1];
            separator[0] = ":";
            String[] dataArray = time.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            result = new DateTime(date.Year,date.Month,date.Day,int.Parse(dataArray[0]), int.Parse(dataArray[1]),int.Parse(dataArray[2]));
            return result;
        }

        public static int GetIndexOfDay(String day)
        {
            day = day[0].ToString().ToUpper() +  day.Substring(1).ToLower();
            int result = -1;
            bool cut = false;
            int i = 0;
            while (!cut)
            {
                if (Enum.ToObject(typeof(DayOfWeek), i).ToString().CompareTo(i.ToString()) == 0)
                {
                    cut = true;
                }
                else
                {
                    if (Enum.ToObject(typeof(DayOfWeek), i).ToString().CompareTo(day) == 0)
                    {
                        cut = true;
                        result = i;
                    }
                }
                i++;
            }
            return result;
        }

        public static bool QuarterChanged(DateTime currentDay, DateTime firstDayCurrentQuarter)
        {
            if (currentDay.Month < 4)
                return (firstDayCurrentQuarter.Month != 1);
            else
                if (currentDay.Month < 7)
                    return (firstDayCurrentQuarter.Month != 4);
                else
                    if (currentDay.Month < 10)
                        return (firstDayCurrentQuarter.Month != 7);
                    else
                        return (firstDayCurrentQuarter.Month != 10);
        }

        public static DateTime CalculateCurrentQuarter(DateTime currentDay)
        {
            DateTime currentQuarter;
            if (currentDay.Month < 4)
                currentQuarter = new DateTime(currentDay.Year, 1, 1);
            else
                if (currentDay.Month < 7)
                    currentQuarter = new DateTime(currentDay.Year, 4, 1);
                else
                    if (currentDay.Month < 10)
                        currentQuarter = new DateTime(currentDay.Year, 7, 1);
                    else
                        currentQuarter = new DateTime(currentDay.Year, 10, 1);

            return currentQuarter;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dateStr"> Jan 1,2009 ; Aug 17,2010 ; etc</param>
        /// <returns></returns>
        public static DateTime ParseDate(String dateStr)
        {
            String myStr = dateStr.Replace(" ", String.Empty);
            String monthStr = myStr.Substring(0, 3);
            myStr = myStr.Substring(3);
            string[] sep = new string[1];
            sep[0] = ",";
            string[] splittedLine = myStr.Split(sep, StringSplitOptions.RemoveEmptyEntries);
            int day = Int32.Parse(splittedLine[0]);
            int year = Int32.Parse(splittedLine[1]);
            int month = (int)((Enumerators.Month)Enum.Parse(typeof(Enumerators.Month), monthStr, true));
            return new DateTime(year, month, day);
        }

        /// <summary>
        /// </summary>
        /// <param name="date"></param>
        /// <returns> Jan09 , Ene10</returns>
        public static String ParseDateTimeToMmmYY(int year, int month, int day, bool useDay, String separator)
        {
            String result = ((Enumerators.Month)month).ToString();
            if (useDay)
            {
                string strDay = day.ToString();
                if (strDay.Length == 1)
                    strDay = "0" + strDay;
                result += separator + strDay;
            }
            result += separator + year.ToString().Substring(2,2);
            return result;
        }
    }
}
